﻿package com.STS.ELearning.Captivate.helpers 
{
	import flash.display.DisplayObjectContainer;
	public class HierarchySearcher
	{
		
		
		public static function getChildrenWithProperty(parent:DisplayObjectContainer, property:String):Array
		{
			var returnArray:Array = new Array();
			
			for (var i:int = 0; i <= parent.numChildren - 1; i++) 
			{
				
				if (parent.getChildAt(i).hasOwnProperty(property))
							returnArray.push(parent.getChildAt(i));
				
			}
			
			return returnArray;
		}
		public static function findParentWithProperty(beginWith:DisplayObjectContainer, property:String, limit:DisplayObjectContainer = null):DisplayObjectContainer
		{
			
		
			if (limit == null) limit = beginWith.stage as DisplayObjectContainer;
			
		
			return cycleUp(beginWith);
			
			function cycleUp (container:DisplayObjectContainer):DisplayObjectContainer {
				if (container.hasOwnProperty(property)) {
					return container;
				} else if (container == limit) {
					
					return null;
					
				} else {
					
					return cycleUp(container.parent);
					
				}
			}
		}
		public static function findParentThatIs(beginWith:DisplayObjectContainer, className:String, limit:DisplayObjectContainer = null):DisplayObjectContainer
		{
			
		
			if (className.charAt(0) != "[") className = "[object " + className + "]";
			
			if (limit == null) limit = beginWith.stage;
			
		
			return cycleUp(beginWith);
			
			function cycleUp (container:DisplayObjectContainer):DisplayObjectContainer {
				
		
				if (container.toString() == className) {
					
					return container;
					
		
				} else if (container == limit) {
					
					return null;
					
		
				} else {
					
					return cycleUp(container.parent);
					
				}
			}
			
		}
		
	}
}